﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinalOnlineLibrary.Models;
using OnlineLibrary.ToLinq;
using FinalOnlineLibrary.DAL;
using MoonLight.Common; 

namespace FinalOnlineLibrary.Controllers
{
    public class BooksController : Controller
    {
        //
        // GET: /Books/
        
        public const int pageSize = 3;
        private IQueryable<tblResource> showBooks;
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// Display and Paging DefaultBooksPage
        /// </summary>
        /// <param name="resType">Get resType parameter from Menu</param>
        /// <param name="page">Get page parameter from PageLinks</param>
        /// <returns></returns>
        public ActionResult DefaultBooksPage(int? resType, int? page) 
        {
            if(Session["Login"]==null)
            {
                Session.Add("Login",false);
                Session.Add("Admin", false);
                Session.Add("Staff", false);
                Session.Add("User", false);
                Session.Add("Guest", true);
            }

            int pageIndex = page ?? 1;
            int checkResType = resType ?? 0;
            string resName = String.Empty;
            if (checkResType == 0)
            {
                showBooks = DALBooks.SelectTopBooks();
                resName = "All of Type";
            }
            else 
            {
                showBooks = DALBooks.SelectTopBooks(checkResType);  
                resName = DALBooks.SelectResTypeName(checkResType);
            }

            var books = PagingBooks(showBooks, pageIndex, pageSize);

            var viewModel = new BooksModel
            {
                ListBooks = books.ToList(),
                PagingInfo = new PagingInfo 
                {
                    CurrentPage = pageIndex,
                    ItemsPerPage = pageSize,
                    TotalItems = showBooks.Count()
                },
                CurrentResType = checkResType,
                CurentResName = resName
            };

            return View(viewModel);
        }

        /// <summary>
        /// Display SearchPage
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ResultSearchPage()
        {
            return View();
        }

        /// <summary>
        /// Get all parameter from Form
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ResultSearchPage(FormCollection form)
        {   
            int? page = 1;
            Session.Add("textSearch", form["txtSearch"].ToString());
            Session.Add("textCatType", form["DDLCatType"].ToString());
            FormInfo formInfo = new FormInfo
            {
                Key = form["txtSearch"].ToString(),
                ResType = int.Parse(form["DDLResType"].ToString()),
                CatType = int.Parse(form["DDLCatType"].ToString())
            };
            return ResultSearchPage(formInfo.Key, formInfo.ResType, formInfo.CatType, page);
        }

        /// <summary>
        /// Paging result search
        /// </summary>
        /// <param name="key"></param>
        /// <param name="resType"></param>
        /// <param name="catType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult ResultSearchPage(string key, int resType, int catType, int? page)
        {
            int pageIndex = page ?? 1;
                      
            showBooks = DALBooks.SearchBooks(key, resType, catType);           
            
            if (showBooks != null)
            {
                var books = PagingBooks(showBooks, pageIndex, pageSize);
                var viewModel = new BooksModel
                {
                    ListBooks = books.ToList(),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = pageIndex,
                        ItemsPerPage = pageSize,
                        TotalItems = showBooks.Count()
                    },
                    CurrentResType = resType
                };
                return View("ResultSearchPage", viewModel);
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// Paging result search
        /// </summary>
        /// <param name="resType">Get resType parameter from PageLinks</param>
        /// <param name="page">Get page parameter from PageLinks</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SearchPage(int? resType, int? page)
        {
            int pageIndex = page ?? 1;

            showBooks = DALBooks.SearchBooks(Session["textSearch"].ToString(), resType ?? 1, Int32.Parse(Session["textCatType"].ToString()));
            if (showBooks != null)
            {
                var books = PagingBooks(showBooks, pageIndex, pageSize);
                var viewModel = new BooksModel
                {
                    ListBooks = books.ToList(),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = pageIndex,
                        ItemsPerPage = pageSize,
                        TotalItems = showBooks.Count()
                    },
                    CurrentResType = resType ?? 1
                };
                return View("ResultSearchPage", viewModel);
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// Use to paging 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IQueryable<tblResource> PagingBooks(IQueryable<tblResource> source,
            int pageIndex, int pageSize)
        {
            return (source.Skip(pageSize * (pageIndex - 1)).Take(pageSize));
        }
    }
}
